package com.jianzhile.jobs.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.jianzhile.jobs.R;

import java.util.HashMap;
import java.util.Map;

public class HttpUtil {
    private static HttpUtil mHttpUtil;
    private RequestQueue mQueue;

    private HttpUtil() {
    }

    /**
     * application中调用
     *
     * @param context
     */
    public void initHttpUtil(Context context) {
        mQueue = Volley.newRequestQueue(context);
    }

    public static HttpUtil getInstance() {
        if (mHttpUtil == null) {
            mHttpUtil = new HttpUtil();
        }
        return mHttpUtil;
    }

    public <T> void addToQueen(Request<T> request) {
        mQueue.add(request);
    }

    /**
     * 回调在主线程中
     *
     * @param listener
     * @param errorListener
     */
    public void requestWithoutSecurity(final Context context,
                                       final Listener<String> listener, final ErrorListener errorListener,
                                       final HashMap<String, String> params, String url, final boolean isShow) {
        // 判断网络状态
        if (CommonUtil.isNetworkAvailable(context) == 0) {
            ToastUtils.showCenterToast(context, R.string.net_eror);
            return;
        }
        if (isShow) {
            showProgressDialog(context);
        }

        StringRequest request = new StringRequest(Method.POST, url,
                new Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        if (isShow) {
                            dismisProgressDialog();
                        }

                        listener.onResponse(response);
                    }
                }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (isShow) {
                    dismisProgressDialog();
                }
                errorListener.onErrorResponse(error);

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };

        addToQueen(request);
    }


    /**
     * 回调在主线程中
     *
     * @param listener
     * @param errorListener
     */
    public void requestWithSecurity(final Context context, final Listener<String> listener,
                                    final ErrorListener errorListener, final HashMap<String, String> params,
                                    String action, final boolean isShow) {
        // 判断网络状态
        if (CommonUtil.isNetworkAvailable(context) == 0) {
            ToastUtils.showCenterToast(context, R.string.net_eror);
            return;
        }
        if (isShow) {
            showProgressDialog(context);
        }
        StringRequest request = new StringRequest(Method.POST, Const.BASE_URL
                + action, new Listener<String>() {

            @Override
            public void onResponse(String response) {
                dismisProgressDialog();
                String responseStr = CommonUtil.deEncry(response);
                listener.onResponse(responseStr);
            }
        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (isShow) {
                    dismisProgressDialog();
                }

                errorListener.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };
        //request.setmParams(params);
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
        addToQueen(request);
    }

    public void requestDirectByGET(final Context context, final Listener<String> listener,
                                   final ErrorListener errorListener,
                                   String url, final boolean isShow) {
        // 判断网络状态
        if (CommonUtil.isNetworkAvailable(context) == 0) {
            ToastUtils.showCenterToast(context, R.string.net_eror);
            return;
        }
        if (isShow) {
            showProgressDialog(context);
        }
        StringRequest request = new StringRequest(Method.GET, url, new Listener<String>() {

            @Override
            public void onResponse(String response) {
                dismisProgressDialog();
                listener.onResponse(response);
            }
        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (isShow) {
                    dismisProgressDialog();
                }

                errorListener.onErrorResponse(error);
            }
        });
        request.setShouldCache(false);
        addToQueen(request);
    }


    /**
     * 回调在主线程中
     *
     * @param listener
     * @param errorListener
     */
    /*public void request(final Listener<String> listener,
			final ErrorListener errorListener, HashMap<String, String> params) {
		StringRequest request = new StringRequest(Method.POST, Const.BASE_URL,
				new Listener<String>() {de

					@Override
					public void onResponse(String response) {

						listener.onResponse(CommonUtil.deEncry(response));
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						errorListener.onErrorResponse(error);
					}
				});

		request.setmParams(params);
		addToQueen(request);
	}*/

    private Dialog mProgressDialog;

    public void showProgressDialog(Context context) {
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
        mProgressDialog = new Dialog(context, R.style.prompt_progress_dailog);
        mProgressDialog.setContentView(R.layout.global_progress_layout);
        mProgressDialog.setCanceledOnTouchOutside(false);
        AnimationDrawable animationDrawable;
        animationDrawable = (AnimationDrawable) ((ImageView) mProgressDialog.getWindow().getDecorView().findViewById(R.id.global_progress_iv)).getDrawable();
        animationDrawable.start();
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }


    public void dismisProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
}
